#ifndef SEQLIST_H
#define SEQLIST_H

#include <iostream>
using namespace std;

#define MAXSIZE 100

template<typename T>
class Seqlist {
private:
    T data[MAXSIZE];
    int length;

public:
    Seqlist() :length(0) {}

    ~Seqlist() {};

    int getLength() const {
        return length;
    }

    bool isEmpty() const {
        return length == 0;
    }

    bool isFull() const {
        return length == MAXSIZE;
    }

    bool insert(int i, const T& element) {
        if (isFull()) {
            cout << "Seqlist is full" << endl;
            return false;
        }
        if (i < 1 || i > length + 1) {
            cout << "This is not a valid position" << endl;
            return false;
        }

        for (int j = length; j >= i; j--) {
            data[j] = data[j - 1];
        }

        data[i - 1] = element;
        length++;
        return true;
    }

    bool remove(int i, T& element) {
        if (isEmpty()) {
            cout << "Seqlist is empty" << endl;
            return false;
        }
        if (i < 1 || i > length) {
            cout << "This is not a valid position" << endl;
            return false;
        }

        element = data[i - 1];
        for (int j = i; j <= length - 1; j++) {
            data[j - 1] = data[j];
        }
        length -= 1;
        return true;
    }

    bool getElement(int i, T& element) const {
        if (i < 1 || i > length) {
            cout << "This is not a valid position" << endl;
            return false;
        }
        element = data[i - 1];
        return true;
    }

    int find(const T& element) {
        for (int j = 0; j < length; j++) {
            if (data[j] == element) {
                return j + 1;
            }
        }
        return 0;
    }

    void display() const {
        if (isEmpty()) {
            cout << "The seqlist is empty" << endl;
            return;
        }
        for (int i = 0; i < length; i++) {
            cout << data[i] << ";";
        }
        cout << endl;
    }

    void clear() {
        length = 0;
    }

    void calculateInsert() {
        if (isEmpty()) {
            cout << "The seqlist is empty" << endl;
            return;
        }
        T sum = 0;
        for (int i = 0; i < length; i++) {
            sum += data[i];
        }
        T average = sum / length;
        int midPlace = length / 2;
        if (length % 2 == 1) {
            midPlace++;
        }
        if (insert(midPlace, average)) {
            cout << "success" << endl;
        }
    }
};

#endif